﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using CarsBll;
using CarsModel;
using System.Web.Http.Results;  
using SencondHandCarBuyAndSell1._0.LibModel;
namespace SencondHandCarBuyAndSell1._0.Controllers
{
    public class GoodsController : ApiController
    {
        GoodsBll bll = new GoodsBll();
        /// <summary>
        /// 卖家上架商品信息
        /// </summary>
        /// <param name="goods">商品信息</param>
        /// <returns></returns>
        [HttpPost]
        public string GoodsAdd([FromBody]Goods goods)
        {
            int i = bll.GoodsADD(goods);
            if (i > 0)
            {
                return "商品上架成功,请等待管理员与您联系";
            }
            else
            {
                return "商品上架失败，请联系管理员";
            }
            
            
        }

        /// <summary>
        /// 管理员完善上架商品信息
        /// </summary>
        /// <param name="Good"></param>
        /// <returns></returns>
        [HttpPost]
        public string GoodsUpdate([FromBody]Goods Good)
        {
            int i = bll.UpdateGoods(Good);
            if (i > 0)
            {
                return "商品已经上架成功";
            }
            else {
                return "商品上架失败，请速与工程师联系";
            }
        }
        /// <summary>
        /// 快速搜索商品信息
        /// </summary>
        /// <param name="GoodsPai">品牌</param>
        /// <param name="MinPrice">最低价</param>
        /// <param name="MaxPrice">最高价</param>
        /// <param name="GoodsMileage">行驶里程</param>
        /// <param name="CarsModel">车型</param>
        /// <param name="PageIndex">当前页</param>
        /// <param name="PageSize">每条显示数据条数</param>
        /// <returns></returns>
        public JsonResult<LibModel.LibModel> GetSelectGoodsList(string GoodsType, decimal MinPrice, decimal MaxPrice, int GoodsMileage, int CarsModel, int PageIndex, int PageSize)
        {
            
            List<Goods> List = bll.GoodsList(GoodsType, MinPrice, MaxPrice, GoodsMileage, CarsModel);
            List<Goods> GoodsList = List.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
            LibModel.LibModel lib = new LibModel.LibModel();
            int i = List.Count();
            if ( i% 9 == 0)
            {
                lib.PageCount = i / 9;
            }
            else
            {
                lib.PageCount = i / 9 + 1;
            }
            lib.Db = GoodsList;
            lib.PageIndex = PageIndex;
            return Json<LibModel.LibModel>(lib);
        }



        /// <summary>
        /// 卖家查询自己上架商品
        /// </summary>
        /// <param name="Userid"></param>
        /// <returns></returns>
        public List<Goods> GetSelectGoodsList(int Userid)
        {
            List<Goods> List = bll.GoodsList(Userid);
            return List;
        }


        /// <summary>
        /// 根据车型查询信息
        /// </summary>
        /// <param name="CarsModelID">车型ID</param>
        /// <returns></returns>
        public List<Goods> GoodsListModel(int CarsModelID)
        {
            List<Goods> List = bll.GoodsListModel(CarsModelID);
            return List;
        }

        /// <summary>
        /// 总页数
        /// </summary>
        /// <returns></returns>

        public JsonResult<LibModel.LibModel> GetBuyCar(int PageIndex, int PageSize)
        {
            List<Goods> List = bll.GoodsList();
            List<Goods> GoodsList = List.Skip((PageIndex-1) * PageSize).Take(PageSize).ToList();
            LibModel.LibModel lib = new LibModel.LibModel();
            int i = List.Count();
            if (i % 9 == 0)
            {
                lib.PageCount = i / 9;
            }
            else
            {
                lib.PageCount = i / 9 + 1;
            }
            lib.Db = GoodsList;
            lib.PageIndex = PageIndex;
            List<LibModel.LibModel> list = new List<LibModel.LibModel>();
            list.Add(lib);
            return Json<LibModel.LibModel>(lib);
        }
        /// <summary>
        /// 分页显示
        /// </summary>
        /// <param name="PageIndex">当前页</param>
        /// <param name="PageSize">每页显示条数</param>
        /// <returns></returns>
        //public List<Goods> GetGoodsList(int PageIndex, int PageSize)
        //{
        //    List<Goods> list = bll.GoodsList(PageIndex, PageSize);
        //    return list;
        //}


        /// <summary>
        /// 商品显示
        /// </summary>
        /// <returns>所有商品</returns>
        public List<Goods> GetGoodsList()
        {

            return bll.GoodsList();
        }

    }
}
